package com.da.lon.wang.xlg.view;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.PopupWindow;

import com.da.lon.wang.xlg.R;
import com.da.lon.wang.xlg.constants.XLGConstants;
import com.da.lon.wang.xlg.util.Utils;
import com.da.lon.wang.xlg.util.WebViewUtil;
import com.da.lon.wang.xlg.util.WebViewUtil.OnLoading;

public class CancelPopWindow extends PopupWindow {

    private Context context = null;
    private View mMenuView;
    private ImageView closeImageView;
    private WebView webView;
    private WebViewUtil webViewUtil;

    public CancelPopWindow(Context context) {
        super(context);

        this.context = context;

        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        mMenuView = inflater.inflate(R.layout.products_cancel_popwindow, null,
                true);

        // 设置SelectPicPopupWindow的View
        this.setContentView(mMenuView);
        // 设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(LayoutParams.FILL_PARENT);
        // 设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(LayoutParams.WRAP_CONTENT);
        // 设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        // 设置SelectPicPopupWindow弹出窗体动画效果
        this.setAnimationStyle(R.style.AnimBottom);
        // 实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0xb0000000);
        // 设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
        // mMenuView添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销毁弹出框
        mMenuView.setOnTouchListener(new OnTouchListener() {

            public boolean onTouch(View v, MotionEvent event) {

                int height = mMenuView.findViewById(R.id.cancel_layout)
                        .getTop();
                int y = (int) event.getY();
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    if (y < height) {
                        dismiss();
                    }
                }
                return true;
            }
        });

        closeImageView = (ImageView) mMenuView.findViewById(R.id.cancel_close);
        closeImageView.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

        webView = (WebView) mMenuView.findViewById(R.id.cancel_webview);

        webView.getSettings().setUseWideViewPort(true);
        webView.getSettings().setLoadWithOverviewMode(true);
        // webView.getSettings().setBuiltInZoomControls(false);

        webViewUtil = new WebViewUtil(context);
        webViewUtil.setWebView(
                Utils.constructUrl(XLGConstants.WEB_URL
                        + "Empty-returntip.html"), null, webView, 0);
        webViewUtil.setOnloadingListener(new OnLoading() {

            @Override
            public void onStart(String url) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onGetLocation(String url) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onError() {
                // TODO Auto-generated method stub

            }

            @Override
            public void onComplete(String url) {
                // TODO Auto-generated method stub

            }
        });
    }

}
